package cn.yunhe.actions;

import cn.yunhe.model.Member;
import cn.yunhe.model.Pagination;
import cn.yunhe.service.MemberBiz;
import cn.yunhe.util.BaseActionUtil;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*Sring注解*/
@Controller(value = "memberAction")
@Scope(value = "prototype")
public class MemberAction extends BaseActionUtil implements ModelDriven {

    /*Spring注解*/
    @Autowired
    @Qualifier("memberBiz")
    private MemberBiz memberBiz;//Service层

    private Member member;//模型驱动
    private int currPageNo;//当前页码
    private int maxRows;//每页显示的最大记录数

    private Pagination<Member>pagination=new Pagination<>();//Json的返回数据类型
    private Map<String,Object>objectMap=new HashMap<>();//多条件筛选查询



    public String searchByConditions(){
        /*s1:多条件筛选查询的总记录数*/
        System.out.println("getMname===="+member.getMname());
        objectMap.put("mname",member.getMname());//为空(未赋值),匹配结果集为空,
        //objectMap.put("mname","");//为空字符串(已赋值),但匹配结果集不为空
        Integer totalRecords= memberBiz.searchByConditions2TotalRecords(objectMap);
        pagination.setTotalRecords(totalRecords);


        /*多条件筛选查询的分页*/
        pagination.setCurrPageNo(currPageNo);
        pagination.setMaxRows(maxRows);

        System.out.println("getTotalRecords======"+pagination.getTotalRecords());
        System.out.println("currPageNo===="+currPageNo);
        System.out.println("maxRows===="+maxRows);
        System.out.println("getTotalPages===="+pagination.getTotalPages());
        System.out.println("getStartIndex===="+pagination.getStartIndex());
        System.out.println("getMaxRows===="+pagination.getMaxRows());

        objectMap.put("startIndex",pagination.getStartIndex());
        objectMap.put("maxRows",pagination.getMaxRows());
        List<Member>memberList=memberBiz.searchByConditions(objectMap);
        pagination.setList(memberList);

        return SUCCESS;
    }

    public MemberBiz getMemberBiz() {
        return memberBiz;
    }

    public void setMemberBiz(MemberBiz memberBiz) {
        this.memberBiz = memberBiz;
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    public int getCurrPageNo() {
        return currPageNo;
    }

    public void setCurrPageNo(int currPageNo) {
        this.currPageNo = currPageNo;
    }

    public int getMaxRows() {
        return maxRows;
    }

    public void setMaxRows(int maxRows) {
        this.maxRows = maxRows;
    }

    public Pagination<Member> getPagination() {
        return pagination;
    }

    public void setPagination(Pagination<Member> pagination) {
        this.pagination = pagination;
    }

    public Map<String, Object> getObjectMap() {
        return objectMap;
    }

    public void setObjectMap(Map<String, Object> objectMap) {
        this.objectMap = objectMap;
    }

    @Override
    public Member getModel() {
        if (member==null)
            member=new Member();
        return member;
    }
}
